<template>
  <div class="wrapper">
    <div class="box">
      <div class="biao">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <div class="form">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
          <!-- 用户名 -->
          <el-form-item prop="username" label="用户名">
            <el-input
              prefix-icon="el-icon-user-solid"
              v-model="ruleForm.username"
              :maxlength="10"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password" label="密码">
            <el-input
              prefix-icon="el-icon-lock"
              v-model="ruleForm.password"
              :maxlength="15"
              type="password"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="buttons">
          <el-button type="primary" @click="userLogin('ruleForm')">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getLogin, getMenus } from "../api/api";
export default {
  name: "login",
  data() {
    return {
      /**
       * 登录loading
       */
      loading: false,
      /**
       * 登录信息
       */
      ruleForm: {
        username: "admin ",
        password: "123456",
      },
      /**
       * 规则
       */
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空",
          },
          {
            min: 3,
            max: 10,
            message: "用户名长度在 3 到 10 个字符",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
          },
          {
            min: 6,
            max: 15,
            message: "密码长度在 6 到 15 个字符",
          },
        ],
      },
    };
  },
  methods: {
    /**
     * 用户登录
     */
    async userLogin(formName) {
      //函数节流
      this.timer=setTimeout(()=>{
          this.$refs[formName].validate(async(valid)=>{
            if(valid){
              let res=await getLogin(this.ruleForm)
              if(res.meta.status==200){
                this.$message.success(res.meta.msg)
                localStorage.setItem("token",res.data.token)
                this.$router.push({name:"home"})
              }
            }
          })
      })
    },
    /**
     * 重置
     */
    reset() {
      this.$refs.form.resetFields();
      this.model = {};
    },
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  height: 100%;
  background: var(--mainBackColor);
  // background-color: rgb(21, 21, 40);
  position: relative;
}
.box {
  width: 450px;
  height: 300px;
  background: white;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;

  transform: translate(-50%, -50%);

  // margin-left: -225px;
  // margin-top: -150px;
  .buttons {
    margin-left: 135px;
  }
  .form {
    box-sizing: border-box;
    width: 100%;
    position: absolute;
    bottom: 20px;
    padding: 0 20px;
  }
}

.biao {
  width: 130px;
  height: 130px;
  background: white;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #ddd;
}
img {
  background: #eee;
  border-radius: 50%;
  width: 100%;
  height: 100%;
}
</style>